//k1编辑

params [["_initial",true]];
private _weatherTemplates = [];
//[overcast,rain,fog,lightnings]

//Clear
_weatherTemplates pushBack [0,0,0,0];

//Overcast
_weatherTemplates pushBack [.6,0,.2,0];
//Rain
_weatherTemplates pushBack [1.0,0,.2,0];
//Fog
_weatherTemplates pushBack [.4,0,[.2+random .2,.01,random 30 + 5],0];
//Storm
_weatherTemplates pushBack [1,.2,.1,1];
//Nightmare
_weatherTemplates pushBack [1,0,[.4,.01,random 20 + 5],1];
//Partly cloudy
_weatherTemplates pushBack [.2,0,0,0];
//Cloudy
_weatherTemplates pushBack [.4,0,0,0];
//Could get rainy
_weatherTemplates pushBack [.6,.1,0,0];
//Nice
_weatherTemplates pushBack [.1,0,0,0];
//Morningmood
_weatherTemplates pushBack [0,0,[.1+random .3,.05,random 5 + 1],0];
//Morningmood2
_weatherTemplates pushBack [0,0,[.1+random .2,.075,random 10 + 5],0];
//Clear
_weatherTemplates pushBack [0,0,0,0];
//Clear
_weatherTemplates pushBack [0,0,0,0];
//Clear
_weatherTemplates pushBack [0,0,0,0];
//Overcast
_weatherTemplates pushBack [.6,0,.2,0];
//Overcast
_weatherTemplates pushBack [.6,0,.2,0];
//Cloudy
_weatherTemplates pushBack [.4,0,0,0];
//Cloudy
_weatherTemplates pushBack [.4,0,0,0];
//Make sure all used vars are initialised
if (isNil "Param_Weather") then {Param_Weather = -1};
if (isNil "Param_DynamicWeather") then {Param_DynamicWeather = 1};
if (isNil "Param_TimeMultiplier") then {Param_TimeMultiplier = 1};
private _currentTemplate = [];
if (Param_Weather < 0) then [{
	_currentTemplate = _weatherTemplates select floor(random(count(_weatherTemplates)));
},{
	if (Param_Weather < count _weatherTemplates) then [{
		_currentTemplate = _weatherTemplates select Param_Weather;
	},{
		_currentTemplate = _weatherTemplates select floor(random(count(_weatherTemplates)));
	}];
}];

private _weatherTransitionTime = 1800;
private _weatherWaitTime = 600;
private _rainDelay = 120; //Delay rainchange
if (_initial) then {_weatherTransitionTime = 0};
if (Param_DynamicWeather == 0) then {_weatherWaitTime = 60*60*24*Param_TimeMultiplier};

//Transition in _weatherTransitionTime ingame seconds
_weatherTransitionTime setOvercast (_currentTemplate select 0);
if (abs(rain-(_currentTemplate select 1)) < .2 || {_initial}) then [{
	_weatherTransitionTime setRain (_currentTemplate select 1);
},{
	//Delay rain a bit of rainchange is to heavy. Otherwise rain will start before clouds appear or vice versa
	[_currentTemplate select 1,_rainDelay/Param_TimeMultiplier,_weatherTransitionTime-(_rainDelay/Param_TimeMultiplier)] spawn {
		params ["_rain","_time","_change"];
		systemchat ("延迟降雨 " + str _time + " 秒");
		sleep _time;
		_change setRain _rain;
	};
}];
_weatherTransitionTime setFog (_currentTemplate select 2);
_weatherTransitionTime setLightnings (_currentTemplate select 3);

systemchat str _currentTemplate;

if (_initial) then {
	skiptime -24;
	skiptime 24;
	forceWeatherChange;
};
//Sleep _weatherTransitionTime ingame seconds
systemchat ("天气变化在 " + str (_weatherTransitionTime/Param_TimeMultiplier) + " 秒");
sleep (_weatherTransitionTime/Param_TimeMultiplier);

//Keep the weather 10 realtime minutes
_weatherWaitTime*Param_TimeMultiplier setOvercast (_currentTemplate select 0);
_weatherWaitTime*Param_TimeMultiplier setRain (_currentTemplate select 1);
_weatherWaitTime*Param_TimeMultiplier setFog (_currentTemplate select 2);
_weatherWaitTime*Param_TimeMultiplier setLightnings (_currentTemplate select 3);

//Keep the weather 10 minutes
systemchat ("保持天气 " + str _weatherWaitTime + " 秒");
sleep _weatherWaitTime;

if (Param_DynamicWeather == 1) then [{
	systemchat "重启天气脚本";
	[false] spawn A3E_fnc_Weather;
},{
	systemchat "保持天气常量";
}];


